﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using NHibernate.Criterion;

using ProjectBase.Core;
using ProjectBase.Data;
using ProjectBase.Data.NH;

using $prjsn$.Common.Data;
using $prjsn$.Common.Data.Services;
using $prjsn$.Entity.Services;

namespace $safeprojectname$.Services
{
    public class ArticleDao : BaseDao<Article, int, ArticleFilter>, IArticleDao
    {
        #region IArticleDao Members
        /// <summary>
        ///     Evaluates all filters and order-by clauses
        ///     and returns sorted list of matching Article entities
        /// </summary>
        /// <param name = "filter">ISearchObject&lt;Article&gt; containing ArticleFilter not-null instance</param>
        /// <returns>List of Article entities matching the filtering criteria</returns>
        public override IEnumerable<Article> Find(ArticleFilter filter)
        {
            Criteria.Append(Apply(filter));

            return GetListByCriteria(filter);
        }

        protected override IList<ICriterion> Apply(ArticleFilter filter)
        {
            var restrictions = base.Apply(filter);

            // ID
            restrictions.AddCompare(filter, f => f.IdExact); // Searches for exact ID
            restrictions.AddIn(filter, f => f.IDColl);

            restrictions.AddEqual(filter, f => f.CodeExact); // Searches for exact Code even string.Empty

            restrictions.AddLike(filter, f => f.Code);
            restrictions.AddLike(filter, f => f.Title);
            restrictions.AddLike(filter, f => f.Description);
            restrictions.AddLike(filter, f => f.Text);

            restrictions.AddEqual(filter, f => f.ArticleCategory);
            restrictions.AddEqual(filter, f => f.ArticleGroup);

            restrictions.AddNotEqual(filter, f => f.ArticleCategoryNot);
            restrictions.AddNotEqual(filter, f => f.ArticleGroupNot);


            restrictions.AddCompare(filter, f => f.IsVisible);

            restrictions.AddCompare(filter, f => f.CreatedFrom);
            restrictions.AddCompare(filter, f => f.CreatedTo);

            restrictions.AddCompare(filter, f => f.UpdatedFrom);
            restrictions.AddCompare(filter, f => f.UpdatedTo);

            restrictions.AddCompare(filter, f => f.ReleaseFrom);
            restrictions.AddCompare(filter, f => f.ReleaseTo);

            return restrictions;
        }

        public IArticleCategoryDao ArticleCategoryDao { protected get; set; }
        public IArticleGroupDao ArticleGroupDao { protected get; set; }
        #endregion
    }
}